package com.guiji.text2video.fastvideo.service.Impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.guiji.text2video.common.core.utils.DateUtils;
import com.guiji.text2video.fastvideo.domain.vo.Text2videoPoolsStat;
import com.guiji.text2video.fastvideo.mapper.Text2videoPoolsStatMapper;
import com.guiji.text2video.fastvideo.service.IText2videoPoolsStatService;
import com.guiji.text2video.fastvideo.utils.Enums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author scl
 * @date 2021-05-27
 */
@Service
public class Text2videoPoolsStatServiceImpl implements IText2videoPoolsStatService {

    private final int zero = 0;
    private final int one = 1;

    @Autowired
    private Text2videoPoolsStatMapper text2videoPoolsStatMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public Text2videoPoolsStat selectText2videoPoolsStatById(Long id) {
        return text2videoPoolsStatMapper.selectText2videoPoolsStatById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param text2videoPoolsStat 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Text2videoPoolsStat> selectText2videoPoolsStatList(Text2videoPoolsStat text2videoPoolsStat) {
        return text2videoPoolsStatMapper.selectText2videoPoolsStatList(text2videoPoolsStat);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param text2videoPoolsStat 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertText2videoPoolsStat(Text2videoPoolsStat text2videoPoolsStat) {
        text2videoPoolsStat.setCreateTime(DateUtils.getNowDate());
        return text2videoPoolsStatMapper.insertText2videoPoolsStat(text2videoPoolsStat);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param text2videoPoolsStat 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateText2videoPoolsStat(Text2videoPoolsStat text2videoPoolsStat) {
        text2videoPoolsStat.setUpdateTime(DateUtils.getNowDate());
        return text2videoPoolsStatMapper.updateText2videoPoolsStat(text2videoPoolsStat);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteText2videoPoolsStatByIds(Long[] ids) {
        return text2videoPoolsStatMapper.deleteText2videoPoolsStatByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteText2videoPoolsStatById(Long id) {
        return text2videoPoolsStatMapper.deleteText2videoPoolsStatById(id);
    }

    @Override
    public Text2videoPoolsStat multiTaskByPool(String ip) {
        return text2videoPoolsStatMapper.multiTaskByPool(ip);
    }

    @Override
    public List<HashMap> selectStat(Text2videoPoolsStat pools) {
        return text2videoPoolsStatMapper.selectStat(pools);
    }

    /**
     * 线程池聚合30秒
     *
     * @param list
     * @return
     */
    @Override
    public List<HashMap> combineByTime(List<HashMap> list, Date time,int gap) {

        List<List<HashMap>> listFin = new ArrayList<>();
        List<HashMap> list60 = new ArrayList<>();
        List<HashMap> res = new ArrayList<>();
        int k=zero;
        Date now = new Date();
        while(time.compareTo(now)<zero){
            if(k<gap){
                if(find(time,list)!=null){
                    list60.add(find(time,list));
                    time = DateUtil.offset(time, DateField.SECOND, one);
                    k++;
                }else{
                    HashMap newMap = createNewMap(time);
                    list60.add(newMap);
                    time = DateUtil.offset(time, DateField.SECOND, one);
                    k++;
                }
            }else {
                k=0;
                listFin.add(list60);
                list60=new ArrayList<>();
            }
        }
        listFin.add(list60);

        for(List<HashMap> list1:listFin){
            if(list1.size()!=zero){
                HashMap map = conmbine(list1,gap);
                res.add(map);
            }
        }

        return res;
    }

    @Override
    public int getCombineGap(Date time) {
        Date now = new Date();
        long l = (now.getTime() - time.getTime()) / 1000;
        if(l>400){
            return (int) (l/100);
        }else{
            return 2;
        }
    }

    private HashMap find(Date time,List<HashMap> list) {
        for(HashMap map:list){
            Date create_time = (Date)map.get("create_time");
            if(time.getTime()/1000==create_time.getTime()/1000){
                return map;
            }
        }
        return null;
    }

    private HashMap createNewMap(Date time){
        HashMap map = new HashMap();
        map.put("NLPActive", Enums.JobSign.RUNNING.getKey());
        map.put("TTSActive", Enums.JobSign.RUNNING.getKey());
        map.put("VideoComposeActive",Enums.JobSign.RUNNING.getKey());
        map.put("TTSQueue",Enums.JobSign.RUNNING.getKey());
        map.put("VideoComposeQueue",Enums.JobSign.RUNNING.getKey());
        map.put("NLPQueue",Enums.JobSign.RUNNING.getKey());
        map.put("create_time",time);
        map.put("label",1);
        return map;
    }

    private HashMap conmbine(List<HashMap> list,int gap){
        int NLPActive=zero;
        int TTSActive=zero;
        int VideoComposeActive=zero;
        int NLPQueue=zero;
        int TTSQueue=zero;
        int VideoComposeQueue=zero;
        for(HashMap map:list){
            NLPActive = Integer.parseInt((String)map.get("NLPActive"))+NLPActive;
            TTSActive = Integer.parseInt((String)map.get("TTSActive"))+TTSActive;
            VideoComposeActive = Integer.parseInt((String)map.get("VideoComposeActive"))+VideoComposeActive;
            NLPQueue = Integer.parseInt((String)map.get("NLPQueue"))+NLPQueue;
            TTSQueue = Integer.parseInt((String)map.get("TTSQueue"))+TTSQueue;
            VideoComposeQueue = Integer.parseInt((String)map.get("VideoComposeQueue"))+VideoComposeQueue;
        }
        DecimalFormat df=new DecimalFormat("0.00");

        //获取gap1
        int gap1 = getGap1(list);

        HashMap res = new HashMap();
        if(gap1==0){
            res.put("NLPActive",zero);
            res.put("TTSActive",zero);
            res.put("VideoComposeActive",zero);
            res.put("NLPQueue",zero);
            res.put("TTSQueue",zero);
            res.put("VideoComposeQueue",zero);
        }else{
            res.put("NLPActive",Math.ceil(Double.parseDouble(df.format((float) NLPActive / gap1))));
            res.put("TTSActive",Math.ceil(Double.parseDouble(df.format((float) TTSActive / gap1))));
            res.put("VideoComposeActive",Math.ceil(Double.parseDouble(df.format((float) VideoComposeActive / gap1))));
            res.put("NLPQueue",Math.ceil(Double.parseDouble(df.format((float) NLPQueue / gap1))));
            res.put("TTSQueue",Math.ceil(Double.parseDouble(df.format((float) TTSQueue /gap1))));
            res.put("VideoComposeQueue",Math.ceil(Double.parseDouble(df.format((float) VideoComposeQueue / gap1))));
        }
        if(list.size()==0){
            res.put("create_time","");
        }else{
            res.put("create_time",list.get(zero).get("create_time"));
        }
        return res;
    }

    private int getGap1(List<HashMap> list){
        int total = list.size();
        for(HashMap map:list){
            if(map.get("label")==null) {
                continue;
            } else {
                total--;
            }
        }
        return total;
    }

    @Override
    public void deleteOneHourAgo(Text2videoPoolsStat pools) {
        text2videoPoolsStatMapper.deleteOneHourAgo(pools);
    }


}
